package com.hxkj.cst.chengshuitong.ui.custom

import android.content.Context
import android.support.v7.widget.AppCompatButton
import android.text.InputType
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import com.hxkj.cst.chengshuitong.R
import kotlinx.android.synthetic.main.custom_input.view.*

/**
 * 作者： Luoyang on 2017/5/26.
 * 描述： 输入框 提供xml复用
 */
class CustomInput(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {

    init {
        inflate(context, R.layout.custom_input, this)
        val resourceType = context.obtainStyledAttributes(attrs, R.styleable.CustomInput)
        val text = resourceType.getResourceId(R.styleable.CustomInput_describe_text, R.string.custom_input_text)
        val hint = resourceType.getResourceId(R.styleable.CustomInput_content_hint, R.string.custom_input_hint)
        val action_visible = resourceType.getInteger(R.styleable.CustomInput_action_visible, GONE)
        val input_type = resourceType.getInteger(R.styleable.CustomInput_input_type, InputType.TYPE_CLASS_TEXT)
        ci_tv_describe.setText(text)
        ci_et_content.setHint(hint)
        ci_et_content.inputType = input_type
        ci_btn_action.visibility = action_visible
        if (input_type == InputType.TYPE_TEXT_VARIATION_PASSWORD)
            ci_et_content.transformationMethod = PasswordTransformationMethod.getInstance()
        if (action_visible == View.VISIBLE) {
            val action_text = resourceType.getResourceId(R.styleable.CustomInput_action_text, R.string.custom_input_action)
            ci_btn_action.setText(action_text)
        }
        resourceType.recycle()
    }

    fun getText(): String = ci_et_content.text.toString()

    fun getAction(): AppCompatButton = ci_btn_action
}